const { default: QRCode } = require("../../resources/util/qrcode/components/QRCode");

cc.Class({
    extends: cc.Component,

    properties: {
        QRCode: cc.Node,
    },


    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo
    },

    start() {
        this.sharePanel = this.node.getChildByName("share_panel");
        this.sharePanel.active = false;
        this.url = "";

        this.panel1 = this.node.getChildByName("right").getChildByName("panel1");
        this.panel1.getChildByName("coin").getChildByName("label").getComponent(cc.Label).string = this.playerInfo.playerCoin;
        this.panel1.getChildByName("pending").getChildByName("label").getComponent(cc.Label).string = "0.00";

        this.pendingRewardData = [];  //待领奖励数据

        this.getAllSons()  //获取所有子玩家
    },

    onClick(event, target) {
        this.lobbyMain.playClickSound();
        if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "close2") {
            this.lobbyMain.hidePanel(this.sharePanel);
        } else if (target == "share") {
            this.showSharePanel();  //处理分享界面
        } else if (target == "copy") {
            this.lobbyMain.copyText(this.url);
        } else if (target == "receive") {  //处理领取界面
            this.showReceivePanel();
        }
    },

    //处理领取界面
    showReceivePanel() {
        this.lobbyMain.openLoading();

        let date = []
        for (let i = 0; i < this.pendingRewardData.length; i++) {
            const element = this.pendingRewardData[i];
            date.push(element.week)
        }
        let data = {
            action: "getRechargeSon",
            playerId: this.playerInfo.playerId,
            date: date
        }
        this.lobbyNet.sendMessage(data);
    },

    //处理分享界面
    showSharePanel() {
        this.url = `http://${this.lobbyMain.configJson.configIp}/?inviterId=${this.playerInfo.playerId}`
        let qrCode = this.QRCode.getComponent(QRCode);
        qrCode.data = this.url;
        this.lobbyMain.showPanel(this.sharePanel);
    },


    //获取所有子玩家
    getAllSons() {
        let data = {
            action: "getAllSons",
            playerId: this.playerInfo.playerId
        }
        this.lobbyNet.sendMessage(data);
        this.lobbyMain.openLoading();
    },

    //获取所有子玩家返回
    onGetAllSons(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            this.pendingRewardData = info.pendingRewards;
            this.setPendingRewards();   //处理待领取的奖励数据
            this.panel1.getChildByName("level1").getChildByName("label").getComponent(cc.Label).string = info.level1;
            this.panel1.getChildByName("level2").getChildByName("label").getComponent(cc.Label).string = info.level2;
            this.panel1.getChildByName("level3").getChildByName("label").getComponent(cc.Label).string = info.level3;
        } else {
            this.lobbyMain.showTips("Failed to retrieve data. Please try again later or log in to the game again.");
        }
    },

    //处理待领取的奖励数据
    setPendingRewards() {

        let amount = 0;
        const ratio = this.playerInfo.ratio;
        amount += this.pendingRewardData.flatMap(element =>
            element.data.map(element2 => element2.amount / ratio)
        ).reduce((sum, current) => sum + current, 0);

        this.panel1.getChildByName('pending').getChildByName("label").getComponent(cc.Label).string = amount;
    },

    // update (dt) {},
});
